package sg.edu.nus.iss.stockex;

import java.text.SimpleDateFormat;
import java.util.*;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;

public class Stock implements Writable, Comparable<Stock> {
	private String code;
	private Date tradeDate;
	private String name;
	private double highPrice;
	private double lowPrice;
	private double openPrice;
	private double closePrice;
	private long volume;

	public Stock()
	{

	}

	public Stock(String code, Date tradeDate, String name, double highPrice,
			double lowPrice, double openPrice, double closePrice, long volume) {
		super();
		this.code = code;
		this.tradeDate = tradeDate;
		this.name = name;
		this.highPrice = highPrice;
		this.lowPrice = lowPrice;
		this.closePrice = closePrice;
		this.openPrice = openPrice;
		this.volume = volume;
	}



	@Override
	public void readFields(DataInput in) throws IOException {
		
		code = Text.readString(in).trim();
		String dateStr = Text.readString(in);
		tradeDate = Util.parseDate(dateStr);
		name = Text.readString(in).trim();
		highPrice = in.readDouble();
		lowPrice = in.readDouble();
		openPrice = in.readDouble();
		closePrice = in.readDouble();
		volume = in.readLong();

	}

	@Override
	public void write(DataOutput out) throws IOException {
		Text.writeString(out, code);
		Text.writeString(out, Util.formatDate(tradeDate));
		Text.writeString(out, name);
		out.writeDouble(highPrice);
		out.writeDouble(lowPrice);
		out.writeDouble(openPrice);
		out.writeDouble(closePrice);
		out.writeLong(volume);
	}

	public static Stock read(DataInput in) throws IOException{
		Stock stock = new Stock();
		stock.readFields(in);
		return stock;
	}

	public static Stock parseString(String stockStr)
	{
		Stock stock = null;
		try
		{
			String[] stockData = stockStr.split(";");
			System.out.println("text:" + stockData.toString());
			String name = stockData[1].trim();
			String code = stockData[14].trim();
			String dateStr = stockData[0].trim();
			Date date = Util.parseDate(dateStr.trim());
			double highPrice =	new Double(stockData[4].trim()).doubleValue();
			double lowPrice = new Double(stockData[5].trim()).doubleValue();
			double openPrice = new Double(stockData[12].trim()).doubleValue();
			double closePrice = new Double(stockData[6].trim()).doubleValue();
			long volume = new Long(stockData[8].trim()).longValue();
			stock = new Stock(code, date, name, highPrice, lowPrice, openPrice, closePrice, volume);
		}catch(Exception ex)
		{
			return null;
		}

		return stock;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Date getTradeDate() {
		return tradeDate;
	}

	public void setTradeDate(Date tradeDate) {
		this.tradeDate = tradeDate;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getHighPrice() {
		return highPrice;
	}

	public void setHighPrice(double highPrice) {
		this.highPrice = highPrice;
	}

	public double getLowPrice() {
		return lowPrice;
	}

	public void setLowPrice(double lowPrice) {
		this.lowPrice = lowPrice;
	}

	public double getClosePrice() {
		return closePrice;
	}

	public void setClosePrice(double closePrice) {
		this.closePrice = closePrice;
	}

	public long getVolume() {
		return volume;
	}

	public void setVolume(long volume) {
		this.volume = volume;
	}

	
	public double getOpenPrice() {
		return openPrice;
	}

	public void setOpenPrice(double openPrice) {
		this.openPrice = openPrice;
	}

	public String getYearMonthTradeDate() {

		return Util.getYearMonth(this.tradeDate);

	}

	@Override
	public int compareTo(Stock other) {
		if (this.getLongTradeDate() < other.getLongTradeDate()) {
			return -1;
		} else if (this.getLongTradeDate() > other.getLongTradeDate()) {
			return 1;
		}
		return 0;
	}

	private long getLongTradeDate() {
		return tradeDate.getTime();
	}
	
	public void copy(Stock source) {

		this.code = source.code;
		this.closePrice = source.closePrice;
		this.lowPrice = source.lowPrice;
		this.highPrice = source.highPrice;
		this.name = source.name;
		this.openPrice = source.openPrice;
		this.tradeDate = source.tradeDate;
		this.volume = source.volume;

	}

}